package com.ordobill.webapp.action;

import java.util.ArrayList;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.commons.logging.*;
import com.ibatis.sqlmap.client.*;
import com.ordobill.webapp.beans.SystemImg;
import com.ordobill.webapp.common.FileManager;
import com.ordobill.webapp.common.SqlMapClientManager;
import org.apache.struts.actions.*;
import org.apache.struts.upload.FormFile;

public class SystemImgAction extends DispatchAction{
    
    private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
    private SqlMapClient sqlMap = SqlMapClientManager.getSqlMapClient();

    @SuppressWarnings("unchecked")
    public ActionForward list(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        
    	SystemImg sysimgForm = (SystemImg)form;        
        ArrayList<SystemImg> sysimgList = (ArrayList<SystemImg>) sqlMap.queryForList("SystemImg.list", sysimgForm);
        int totalCount = (Integer) sqlMap.queryForObject("SystemImg.listCount", sysimgForm);
        
        request.setAttribute("sysimgList", sysimgList);
        request.setAttribute("sysimgForm", sysimgForm);
        request.setAttribute("totalCount", totalCount);
        
        return mapping.findForward("list");
    }

    public ActionForward info(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        
    	SystemImg sysimgForm = (SystemImg)form;        
    	SystemImg sysimgData = (SystemImg) sqlMap.queryForObject("SystemImg.list", sysimgForm);
        
        request.setAttribute("sysimgData", sysimgData);
        request.setAttribute("sysimgForm", sysimgForm);
        
        return mapping.findForward("info");
    }
    
    public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        
    	  SystemImg sysimgForm = (SystemImg)form;
    	  log.debug("File Size is ===============>"+sysimgForm.getUpFile().getFileSize());
    	  if(sysimgForm.getUpFile().getFileSize() > 0){
    		  
    		  try{
    			  sqlMap.startTransaction();
    			  sqlMap.update("SystemImg.systemimgUpdate", sysimgForm);
    			  FormFile uploadFile = sysimgForm.getUpFile();
    			  FileManager.fileUpload(uploadFile, sysimgForm.getSysimgUid(), sysimgForm.getSysimgGubun());
    			  
    			  /**
    			   * 파일을 삭제합니다.
    			   */
    			  int isDelete = FileManager.fileDelete(sysimgForm.getSysimgFile());
    			  
    			  /**
    			   * 파일이 정상적으로 삭제되었다면 isDelete값은 0이상됩니다.
    			   */
    			  if(isDelete < 0){
    				  throw new Exception("기존 파일이 삭제 되지 않았습니다.");
    			  }
    			  
    			  sqlMap.commitTransaction();
    			  
    			  ActionMessages messages = new ActionMessages();
    			  messages.add("message", new ActionMessage("수정에 성공했습니다."));
    			  saveMessages(request, messages);    			  
    		  }catch(Exception e){
    			  
    			  log.error(e);
    			  
    			  ActionMessages errors = new ActionMessages();
    			  errors.add("message", new ActionMessage("수정에 실패했습니다."));
    			  saveMessages(request, errors);    		  
    		  }finally{
    			  sqlMap.endTransaction(); 
    		  }
    		  
    	  }
    	  
    	  if(sysimgForm.getUpFile().getFileSize() <= 0 || sysimgForm.getUpFile() == null){
    		  sqlMap.update("SystemImg.systemimgUpdate", sysimgForm);
			  ActionMessages errors = new ActionMessages();
			  errors.add("message", new ActionMessage("수정에 성공했습니다."));
			  saveMessages(request, errors); 
    	  }
    	  sysimgForm.setSysimgName("");
    	  SystemImg sysimgData = (SystemImg) sqlMap.queryForObject("SystemImg.list", sysimgForm);
    	  request.setAttribute("sysimgData", sysimgData);
    	  return mapping.findForward("update");
    }
    
    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
    	SystemImg sysimgForm = (SystemImg)form;
    	int sysimgUid = (Integer) sqlMap.delete("SystemImg.systemimgDelete", sysimgForm);
    	
    	if(sysimgUid > 0){
    		int isDelete = FileManager.fileDelete(sysimgForm.getSysimgFile());
    		
    		if(isDelete > 0){
    			ActionMessages messages = new ActionMessages();
    			messages.add("message", new ActionMessage("삭제 되었습했습니다."));
    			saveMessages(request, messages);
    		}else if(isDelete < 0){
    			ActionMessages messages = new ActionMessages();
    			messages.add("message", new ActionMessage("삭제에 실패했습니다."));
    			saveMessages(request, messages);    			
    		}
    		
    	}else if(sysimgUid <= 0){
			ActionMessages messages = new ActionMessages();
			messages.add("message", new ActionMessage("삭제에 실패했습니다."));
			saveMessages(request, messages);    		
    	}
    	
  	  //SystemImg sysimgData = (SystemImg) sqlMap.queryForObject("SystemImg.list", sysimgForm);
	  request.setAttribute("sysimgData", sysimgForm);
    	
    	return mapping.findForward("delete");
    }
    
    public ActionForward insert(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        
    	SystemImg sysimgForm = (SystemImg)form;
        try{
        	sqlMap.startTransaction();
        	int sysimgUid = (Integer) sqlMap.insert("SystemImg.systemimgInsert", sysimgForm);
        	FormFile uploadFile = sysimgForm.getUpFile();
        	FileManager.fileUpload(uploadFile, sysimgUid, sysimgForm.getSysimgGubun());
        	
        	sqlMap.commitTransaction();
        	
			ActionMessages messages = new ActionMessages();
			messages.add("message", new ActionMessage("저장에 성공했습니다."));
			saveMessages(request, messages);
        	
        }catch(Exception e){
			log.error(e);
			
			ActionMessages errors = new ActionMessages();
			errors.add("message", new ActionMessage("저장에 실패했습니다."));
			saveMessages(request, errors);
        	
        }finally{
        	sqlMap.endTransaction();
        }
        
        return mapping.findForward("success");
    }    
}